package com.shuyuntu.proposal.service;

import com.shuyuntu.proposal.dao.IDelegateDao;
import com.shuyuntu.proposal.dao.IPermissionDao;
import com.shuyuntu.proposal.entity.Delegate;
import com.shuyuntu.proposal.entity.Permission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * Created by syt005 on 2016/4/13.
 */
@Service("delegateService")
public class DelegateService {

    @Transactional
    public boolean loginCheck(String userId, String userPwd) {
        Delegate delegate = delegateDao.queryById(userId);

        if (delegate == null || !delegate.getPassword().equals(userPwd)) {
            return false;
        }

        return true;
    }

    @Transactional
    public Delegate getDelegateById(String userId) {
        Delegate delegate = delegateDao.queryById(userId);

        return delegate;
    }

    @Transactional
    public boolean checkPermission(String userId, Integer permissionId) {
        Delegate delegate = delegateDao.queryById(userId);
        Permission permission = permissionDao.queryById(permissionId);
        Set<Permission> permissions = delegate.getRole().getPermissions();
        if (permissions.contains(permission)) {
            return true;
        }

        return false;
    }

    @Transactional
    public List<Delegate> getAllDelegateInfo() {
        return delegateDao.queryList();
    }

    /******************依赖注入******************/
    @Resource
    IDelegateDao delegateDao;

    public IDelegateDao getDelegateDao() {
        return delegateDao;
    }

    public void setDelegateDao(IDelegateDao delegateDao) {
        this.delegateDao = delegateDao;
    }
    @Resource
    IPermissionDao permissionDao;

    public IPermissionDao getPermissionDao() {
        return permissionDao;
    }

    public void setPermissionDao(IPermissionDao permissionDao) {
        this.permissionDao = permissionDao;
    }

}
